//=============================================================================
// RPG Maker MZ - findMoveTo
//=============================================================================

/*:
 * @target MZ
 * @plugindesc 事件移动追逐的加强
 * @author wangwang
 *
 * @help 
 * 事件移动追逐的加强
 * 移动路线中使用
 * this.moveDirectionTo(x,y) 向该目的地移动
 * this.moveDirectionToPlayer() 向玩家移动
 * this.moveDirectionToFollower(id) 向跟随者移动
 * this.moveDirectionToEvent(id) 向id的事件  
 * this.moveDirectionToVehicle(id) 向id的交通工具移动 
 * 0  "boat", 1   "ship",2  "airship" 
 * 
 * 移动到xy的一步
 * this.moveTo(x ) 
 *    x > 0 事件
 *    x == 0 玩家
 *    x < 0 随从
 *    x == "boat" "ship" "airship"  交通工具
 *    第二个参数 y 存在 移动到x,y
 * 
 * 
 * this.findDirectionTo(x,y) 向该目的地移动的方向
 * this.findDirectionToPlayer() 向玩家移动的方向
 * this.findDirectionToFollower(id) 向跟随者移动的方向
 * this.findDirectionToEvent(id) 向id的事件的方向
 * this.findDirectionToVehicle(id) 向id的交通工具移动的方向
 * 0  "boat", 1   "ship",2  "airship" 
 * 
 * 返回移动到xy的一步的方向
 * this.findTo(x ) 
 *    x > 0 事件
 *    x == 0 玩家
 *    x < 0 随从
 *    x == "boat" "ship" "airship"  交通工具 
 *    第二个参数 y 存在 移动到x,y  
 * 
 * */

Game_Character.prototype.findDirectionToCharacter = function (character) {
    if (character) {
        try {
            return this.findDirectionTo(character.x, character.y)
        } catch (error) {
        }
    }
    return 0
}

Game_Character.prototype.findDirectionToEvent = function (id) {
    const character = $gameMap.event(id)
    return this.findDirectionToCharacter(character)
}



Game_Character.prototype.findDirectionToPlayer = function () {
    const character = $gamePlayer
    return this.findDirectionToCharacter(character)
}



Game_Character.prototype.findDirectionToFollower = function (id) {
    const character = $gamePlayer._followers.follower(id)
    return this.findDirectionToCharacter(character)
}


Game_Character.prototype.findDirectionToVehicle = function (id) {
    const character = $gameMap.vehicle(id)
    return this.findDirectionToCharacter(character)
}


/**
 * 寻找移动方向
 * @param {*} x 
 * @param {*} y 
 */
Game_Character.prototype.findTo = function (x, y) {
    if (y !== void 0) {
        this.findDirectionTo(x, y)
    } else if (typeof x == "string") {
        this.findDirectionToVehicle(x)
    } else if (x > 0) {
        this.findDirectionToEvent(x)
    } else if (x == 0) {
        this.findDirectionToPlayer(x)
    } else {
        this.findDirectionToFollower(-x)
    }
}



/**
 * 寻找移动方向并移动
 * @param {*} x 
 * @param {*} y 
 */

Game_Character.prototype.moveTo = function (x, y) {
    if (y !== void 0) {
        this.moveDirectionTo(x, y)
    } else if (typeof x == "string") {
        this.moveDirectionToVehicle(x)
    } else if (x > 0) {
        this.moveDirectionToEvent(x)
    } else if (x == 0) {
        this.moveDirectionToPlayer(x)
    } else {
        this.moveDirectionToFollower(-x)
    }
}

/**
 * 移动到 目标点
 * @param {*} x 
 * @param {*} y 
 */

Game_Character.prototype.moveDirectionTo = function (x, y) {
    let direction = this.findDirectionTo(x, y)
    direction &&  this.moveStraight(direction);
}

/**
 * 向角色移动
 * @param {*} character 
 */
Game_Character.prototype.moveDirectionToCharacter = function (character) {
    let direction = this.findDirectionToCharacter(character)
    direction && this.moveStraight(direction);
}




/**
 * 向事件移动
 * @param {*} id 
 */
Game_Character.prototype.moveDirectionToEvent = function (id) {
    const character = $gameMap.event(id)
    this.moveDirectionToCharacter(character)
}



/**
 * 向玩家移动 
 * @param {*} id 
 */
Game_Character.prototype.moveDirectionToPlayer = function () {
    const character = $gamePlayer
    this.moveDirectionToCharacter(character)
}


/**
 * 向跟随者移动
 * @param {*} id 
 */
Game_Character.prototype.moveDirectionToFollower = function (id) {
    const character = $gamePlayer._followers.follower(id)
    this.moveDirectionToCharacter(character)
}


/**
 * 向交通工具移动
 */
Game_Character.prototype.moveDirectionToVehicle = function (id) {
    const character = $gameMap.vehicle(id)
    this.moveDirectionToCharacter(character)
}

